package com.yixin.vuehr.web.controller.emp;

import com.yixin.vuehr.bean.RespBean;
import com.yixin.vuehr.bean.RespPageBean;
import com.yixin.vuehr.util.POIUtils;
import com.yixin.vuehr.model.Department;
import com.yixin.vuehr.model.Employee;
import com.yixin.vuehr.model.JObLevel;
import com.yixin.vuehr.model.Nation;
import com.yixin.vuehr.model.Politicsstatus;
import com.yixin.vuehr.model.Position;
import com.yixin.vuehr.service.DepartmentService;
import com.yixin.vuehr.service.EmployeeService;
import com.yixin.vuehr.service.JObLevelService;
import com.yixin.vuehr.service.NationService;
import com.yixin.vuehr.service.PoliticsstatusService;
import com.yixin.vuehr.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/employee/basic")
public class EmpBasicController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    JObLevelService jObLevelService;
    @Autowired
    PositionService positionService;
    @Autowired
    DepartmentService departmentService;
    @GetMapping("/")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10")Integer size,
                                          Employee employee, Date[] bDateScope){
        return employeeService.getEmployeeByPage(page,size,employee,bDateScope);
    }

    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee){
        if (employeeService.addEmp(employee) == 1){
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee){
        if (employeeService.updateEmp(employee) == 1){
            return RespBean.ok("跟新成功！");
        }
        return RespBean.error("跟新失败！");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteEmpById(@PathVariable Integer id){
        if (employeeService.deleteEmpById(id)==1){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @GetMapping("/nation")
    public List<Nation> getAllNation(){
        return nationService.getAllNation();
    }
    @GetMapping("/politicsstatus")
    public List<Politicsstatus> getAllPoliticsstatus(){
        return politicsstatusService.getAllPoliticsstatus();
    }
    @GetMapping("/jObLevel")
    public List<JObLevel> getAllJObLevel(){
        return jObLevelService.getAllJObLevels();
    }
    @GetMapping("/position")
    public List<Position> getAllPosition(){
        return positionService.getAllPositions();
    }
    @GetMapping("/maxWorkId")
    public RespBean maxWorkId(){
        String format = String.format("%08d", employeeService.maxWorkId() + 1);
        RespBean respBean = RespBean.build().setStatus(200).setObj(format);
        return respBean;
    }
    @GetMapping("/deps")
    public List<Department> getAllDeps(){
        return departmentService.getAllDepartments();
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData(){
        List<Employee> list = (List<Employee>) employeeService.getEmployeeByPage(null,null,null,null).getData();
        return POIUtils.employee2Excle(list);
    }

    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
//        file.transferTo(new File("D:\\yixinge.xls"));
        List<Employee> list = POIUtils.excel2Employee(file,nationService.getAllNation(),politicsstatusService.getAllPoliticsstatus(),departmentService.getAllDepartmentsWithOutChildren(),
                positionService.getAllPositions(),jObLevelService.getAllJObLevels());
        if (employeeService.addEmps(list) == list.size()){
            return RespBean.ok("上传成功");
        }
        return RespBean.error("上传失败");
    }

}
